//
//  GridViewController.m
//  Rainbow
//
//  Created by Alex on 28/07/09.
//  Copyright 2009 Erasmus Software. All rights reserved.
//

#import "GridViewController.h"
#import "dvd.h"
#import "itemGridDVD.h"


@implementation GridViewController

- (id) init
{
	if (![super initWithNibName:@"GridView" bundle:nil])
		return nil;
	[self setTitle:@"Grid View"];
	browser = [[DvdGrid alloc] init];
	return self;
}

-(DvdGrid*)DvdGrid
{
	return browser;
}

-(void)setDvdList:(Dvd*)DvdList
{
	dvd_list = DvdList;
}

- (NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *)aBrowser
{
	return [dvd_list count];
}

- (id) imageBrowser:(IKImageBrowserView *)aBrowser itemAtIndex:(NSUInteger)index
{
	NSImage		*img;
	NSDictionary *dico = [[NSDictionary alloc] init];
	
	dico = [dvd_list itemAtIndex:index];
	
	if ([[dico objectForKey:kPath] hasPrefix:@"http:"] == NO)
		img = [[NSImage alloc] initWithContentsOfFile:[dico objectForKey:kPath]];
	else
		img = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:[dico objectForKey:kPath]]];
	itemGridDVD *item = NULL;
	
	item = [[itemGridDVD alloc] initWithImage:img 
										title:[[dvd_list itemAtIndex:index] objectForKey:kTitle] 
										director:[[dvd_list itemAtIndex:index] objectForKey:kDirector]
										Id:[[dvd_list itemAtIndex:index] objectForKey:kId]];

	return item;
	
}

- (void) imageBrowser:(IKImageBrowserView *) aBrowser removeItemsAtIndexes:(NSIndexSet *) indexes
{
	for (int i = [indexes firstIndex]; i < [indexes lastIndex]; i++)
		[dvd_list removeItemAtIndex:i];
}



- (void) dealloc
{
	[browser release];
	[super dealloc];
}

@end
